package com.ohos.carbon.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.agp.components.element.ElementScatter;
import ohos.agp.utils.Color;
import ohos.multimodalinput.event.TouchEvent;

import java.util.Map;

import static com.ohos.carbon.ResourceTable.*;
import static com.ohos.carbon.slice.MainAbilitySlice.pf;
import static ohos.multimodalinput.event.TouchEvent.PRIMARY_POINT_DOWN;
import static ohos.multimodalinput.event.TouchEvent.PRIMARY_POINT_UP;

public class SVGiconsSlice extends AbilitySlice implements Component.ClickedListener {
    private Image back_button, debug, checkbox, image;
    private Text text_title, text;
    private DirectionalLayout directionalLayout;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(Layout_ability_svgIcons);
        initView();
        initRippleColor();

        MainAbilitySlice.all = (Map<String, Integer>) pf.getAll();
        if (MainAbilitySlice.all.size() != 0) {
            if (MainAbilitySlice.all.containsKey("SVG icons")) {
                for (Map.Entry<String, Integer> entry : MainAbilitySlice.all.entrySet()) {
                    if (entry.getKey().equals("SVG icons")) {
                        int integer = entry.getValue();
                        integer++;
                        pf.putInt("SVG icons", integer);
                        pf.flushSync();
                    }
                }
            } else {
                pf.putInt("SVG icons", 1);
                pf.flushSync();
            }
        } else {
            pf.putInt("SVG icons", 1);
            pf.flushSync();
        }
    }

    private void initRippleColor() {
        directionalLayout = (DirectionalLayout) findComponentById(Id_directionalLayout);
        directionalLayout.setTouchEventListener(new Component.TouchEventListener() {
            @Override
            public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
                int action = touchEvent.getAction();
                switch (action) {
                    case PRIMARY_POINT_UP:
                        directionalLayout.setBackground(ElementScatter.getInstance(getContext()).parse(Graphic_background_color_white));
                        image.setImageElement(ElementScatter.getInstance(getContext()).parse(Graphic_ic_today_24px));
                        text.setTextColor(new Color(0xFF000000));
                        break;
                    case PRIMARY_POINT_DOWN:
                        directionalLayout.setBackground(ElementScatter.getInstance(getContext()).parse(Graphic_background_color_gray));
                        image.setImageElement(ElementScatter.getInstance(getContext()).parse(Graphic_ic_today_other_24px));
                        text.setTextColor(new Color(0xFF5A150F));
                        break;
                }
                return true;
            }
        });
    }

    private void initView() {
        image = (Image) findComponentById(Id_image);
        text = (Text) findComponentById(Id_text_press);
        back_button = (Image) findComponentById(Id_back_button);
        back_button.setClickedListener(this);
        debug = (Image) findComponentById(Id_image1);
        debug.setClickedListener(this);
        checkbox = (Image) findComponentById(Id_image2);
        checkbox.setClickedListener(this);
        text_title = (Text) findComponentById(Id_title_text);
        text_title.setText("SVG icons");
        back_button.setImageElement(ElementScatter.getInstance(getContext()).parse(Graphic_carbon_uparrow));
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {
        if (component.getId() == Id_back_button) {
            onBackPressed();
        } else if (component.getId() == Id_image1) {

        } else if (component.getId() == Id_image2) {

        }
    }

    @Override
    protected void onBackPressed() {
        super.onBackPressed();
    }
}
